#include "io_base.h"
#include "def.h"

int echo_client(void)
{
	sock_t sock;

	if (sock_init(&sock, ">tcp:127.0.0.1:8888") < 0) {
		ERR("sock_init fail");
		goto _out;
	}

	char buf[1024];
	size_t len = 0;
	int ret = -1;
	while (1) {
		memset(buf, 0, sizeof(buf));
		printf("\n>>:");
		if (!gets(buf)) {
			DBG("EXIT");
			break;
		}

		len = strlen(buf);
		if (len == 0)
			continue;

		if (sock_send(&sock, buf, len) < len) {
			DBG("sock_send fail");
			break;
		}
		printf("sock_send %d bytes: %s\n", len, buf);

		memset(buf, 0, sizeof(buf));
		ret = sock_receive(&sock, buf, sizeof(buf));
		printf("sock_receive %d bytes:\n", ret);
		if (ret <= 0)
			break;

		if (ret > 0)
			printf("\t%s", buf);
	}

	printf("Done\n");
_out:
	sock_term(&sock);
	return 0;
}

int main(int argc, char **argv)
{
	return echo_client();
}
